package main

import (
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"math/rand"
	"time"
)

func main() {
	r := gin.Default()

	logger, err2 := zap.NewProduction()
	if err2 != nil {
		panic(err2)
	}
	// mid
	r.Use(func(c *gin.Context) {

		startTime := time.Now()
		c.Next()
		logger.Info("incoming request",
			// path
			zap.String("path", c.Request.URL.Path),
			// status
			zap.Int("status", c.Writer.Status()),
			// time
			zap.Duration("time", time.Now().Sub(startTime)),
		)
	}, func(c *gin.Context) {
		// requestParam
		c.Set("requestId", rand.Int())
	})

	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{"message": "pong"})
	})

	r.GET("/hello", func(c *gin.Context) {
		c.String(200, "world")
	})

	err := r.Run()
	if err != nil {
		return
	}
}
